#!/bin/bash


Set_DB_Password()
{
#set mysql root password

    DB_Root_Password="123456"
    Echo_Yellow "Please setup root password of MySQL/MariaDB.(Default password: 123456)"
    read -p "Please enter: " DB_Root_Password
    if [ "${DB_Root_Password}" = "" ]; then
        DB_Root_Password="123456"
    fi
    echo -e " \e[0;32mMySQL/MariaDB root password: \e[0;31m${DB_Root_Password}\e[0m"
}

Storage_Selection()
{
    #do you want to enable or disable the InnoDB Storage Engine?
    echo "==========================="

    InstallInnodb="y"
    Echo_Yellow "Do you want to enable or disable the InnoDB Storage Engine?"
    read -p "Default enable,Enter your choice [Y/n]: " InstallInnodb

    case "${InstallInnodb}" in
        [yY][eE][sS]|[yY])
            echo -e " \e[0;32mYou will \e[0;31menable\e[0;32m the InnoDB Storage Engine!\e[0m"
            ;;
        [nN][oO]|[nN])
            echo -e " \e[0;32mYou will \e[0;31mdisable\e[0;32m the InnoDB Storage Engine!\e[0m"
            ;;
        *)
            echo -e " \e[0;32mNo input,The InnoDB Storage Engine will \e[0;31menable\e[0m"
            InstallInnodb="y"
    esac
}

DB_Selection()
{
    #which MySQL Version do you want to install?
    echo "==========================="

    DBSelect="8"
    Echo_Yellow "You have many options for your DataBase install."
    echo -e "   \e[0;31m1\e[0m: Install MySQL ${MySQL_Version1}"
    echo -e "   \e[0;31m2\e[0m: Install MySQL ${MySQL_Version2}"
    echo -e "   \e[0;31m3\e[0m: Install MySQL ${MySQL_Version3}"
    echo -e "   \e[0;31m4\e[0m: Install MySQL ${MySQL_Version4}"
    [[ "${MySQL_Version5}" = "" ]] || echo -e "   \e[0;31m5\e[0m: Install MySQL ${MySQL_Version5}"
    echo -e "   \e[0;31m6\e[0m: Install MariaDB ${MariaDB_Version6}"
    echo -e "   \e[0;31m7\e[0m: Install MariaDB ${MariaDB_Version7}"
    echo -e "   \e[0;31m8\e[0m: Install MariaDB ${MariaDB_Version8}"
    echo -e "   \e[0;31m9\e[0m: Install MariaDB ${MariaDB_Version9} \e[44;37m(Default)\e[0m"
    [[ "${MariaDB_Version0}" = "" ]] || echo -e "   \e[0;31m0\e[0m: Install MariaDB ${MariaDB_Version0}"
    read -p "Enter your choice (1, 2, 3, 4, 5, 6, 7, 8, 9 or 0): " DBSelect
    case "${DBSelect}" in
        1)
            echo -e " \e[0;32mYou will install \e[0;31mMySQL ${MySQL_Version1}\e[0m"
            ;;
        2)
            echo -e " \e[0;32mYou will install \e[0;31mMySQL ${MySQL_Version2}\e[0m"
            ;;
        3)
            echo -e " \e[0;32mYou will install \e[0;31mMySQL ${MySQL_Version3}\e[0m"
            ;;
        4)
            echo -e " \e[0;32mYou will install \e[0;31mMySQL ${MySQL_Version4}\e[0m"
            ;;
        5)
            echo -e " \e[0;32mYou will install \e[0;31mMySQL ${MySQL_Version5}\e[0m"
            ;;
        6)
            echo -e " \e[0;32mYou will install \e[0;31mMariaDB ${MariaDB_Version6}\e[0m"
            ;;
        7)
            echo -e " \e[0;32mYou will install \e[0;31mMariaDB ${MariaDB_Version7}\e[0m"
            ;;
        8)
            echo -e " \e[0;32mYou will install \e[0;31mMariaDB ${MariaDB_Version8}\e[0m"
            ;;
        9)
            echo -e " \e[0;32mYou will install \e[0;31mMariaDB ${MariaDB_Version9}\e[0m"
            ;;
        0)
            echo -e " \e[0;32mYou will install \e[0;31mMariaDB ${MariaDB_Version0}\e[0m"
            ;;
        *)
            if  [ "${DBSelect}" = "" ]; then   
                echo -e " \e[0;32mNo input,You will install \e[0;31mMariaDB ${MariaDB_Version9}\e[0m"
             else
                echo -e " \e[0;32mYour Selection is wrong,You will default install \e[0;31mMariaDB ${MariaDB_Version9}\e[0m"
            fi
            DBSelect="9"
    esac
    if ([ ${DBSelect} -ge 3 ] && [ ${DBSelect} -lt 6 ]) || [ ${DBSelect} -ge 7 ]; then
        MemTotal=`free -m | grep Mem | awk '{print  $2}'`
        if [[  ${MemTotal} -lt 960 ]]; then
            Echo_Red "warning:"
            echo "    Your Memory is: ${MemTotal} MB less than 1GB, can't install MySQL 5.6+ or MariaDB 10.0+"
            echo "    Please reselection"
            DB_Selection
        fi
    fi
    if [ ${DBSelect} -ge 6 ]; then
        MySQL_Bin="/usr/local/mariadb/bin/mysql"
        MySQL_Config="/usr/local/mariadb/bin/mysql_config"
        MySQL_Dir="/usr/local/mariadb"
     else
        MySQL_Bin="/usr/local/mysql/bin/mysql"
        MySQL_Config="/usr/local/mysql/bin/mysql_config"
        MySQL_Dir="/usr/local/mysql"
    fi
}

PHP_Selection()
{
    #which PHP Version do you want to install?
    echo "==========================="

    PHPSelect="8"
    Echo_Yellow "You have many options for your PHP install."
    echo -e "   \e[0;31m1\e[0m: Install PHP ${PHP_Version1}"
    echo -e "   \e[0;31m2\e[0m: Install PHP ${PHP_Version2}"
    echo -e "   \e[0;31m3\e[0m: Install PHP ${PHP_Version3}"
    echo -e "   \e[0;31m4\e[0m: Install PHP ${PHP_Version4}"
    echo -e "   \e[0;31m5\e[0m: Install PHP ${PHP_Version5}"
    echo -e "   \e[0;31m6\e[0m: Install PHP ${PHP_Version6}"
    echo -e "   \e[0;31m7\e[0m: Install PHP ${PHP_Version7}"
    echo -e "   \e[0;31m8\e[0m: Install PHP ${PHP_Version8} \e[44;37m(Default)\e[0m"
    [[ "${PHP_Version9}" = "" ]] || echo -e "   \e[0;31m9\e[0m: Install PHP ${PHP_Version9}"
    [[ "${PHP_Version0}" = "" ]] || echo -e "   \e[0;31m9\e[0m: Install PHP ${PHP_Version0}"
    read -p "Enter your choice (1, 2, 3, 4, 5, 6, 7, 8 or 9): " PHPSelect

    case "${PHPSelect}" in
        1)
            echo -e " \e[0;32mYou will install \e[0;31mPHP ${PHP_Version1}\e[0m"
            ;;
        2)
            echo -e " \e[0;32mYou will install \e[0;31mPHP ${PHP_Version2}\e[0m"
            ;;
        3)
            echo -e " \e[0;32mYou will install \e[0;31mPHP ${PHP_Version3}\e[0m"
            ;;
        4)
            echo -e " \e[0;32mYou will install \e[0;31mPHP ${PHP_Version4}\e[0m"
            ;;
        5)
            echo -e " \e[0;32mYou will install \e[0;31mPHP ${PHP_Version5}\e[0m"
            ;;
        6)
            echo -e " \e[0;32mYou will install \e[0;31mPHP ${PHP_Version6}\e[0m"
            ;;
        7)
            echo -e " \e[0;32mYou will install \e[0;31mPHP ${PHP_Version7}\e[0m"
            ;;
        8)
            echo -e " \e[0;32mYou will install \e[0;31mPHP ${PHP_Version8}\e[0m"
            ;;
        9)
            echo -e " \e[0;32mYou will install \e[0;31mPHP ${PHP_Version9}\e[0m"
            ;;
        0)
            echo -e " \e[0;32mYou will install \e[0;31mPHP ${PHP_Version0}\e[0m"
            ;;
        *)
            echo -e " \e[0;32mNo input,You will install \e[0;31mPHP ${PHP_Version8}\e[0m"
            PHPSelect="8"
    esac
}

Malloc_Selection()
{
    #which Memory Allocator do you want to install?
    echo "==========================="

    SelectMalloc="1"
    Echo_Yellow "You have 3 options for your Memory Allocator install."
    echo -e "   \e[0;31m1\e[0m: Don't install Memory Allocator. \e[44;37m(Default)\e[0m"
    echo -e "   \e[0;31m2\e[0m: Install Jemalloc"
    echo -e "   \e[0;31m3\e[0m: Install TCMalloc"
    read -p "Enter your choice (1, 2 or 3): " SelectMalloc

    case "${SelectMalloc}" in
        1)
            echo -e " \e[0;32mYou will not install Memory Allocator.\e[0m"
            ;;
        2)
            echo -e " \e[0;32mYou will install \e[0;31mJeMalloc\e[0m"
            ;;
        3)
            echo -e " \e[0;32mYou will install \e[0;31mTCMalloc\e[0m"
            ;;
        *)
            echo -e " \e[0;32mNo input,You will not install Memory Allocator.\e[0m"
            SelectMalloc="1"
    esac

    if [ "${SelectMalloc}" =  "1" ]; then
        MySQL51MAOpt=''
        MySQL55MAOpt=''
        NginxMAOpt=''
     elif [ "${SelectMalloc}" =  "2" ]; then
        MySQL51MAOpt='--with-mysqld-ldflags=-ljemalloc'
        MySQL55MAOpt="-DCMAKE_EXE_LINKER_FLAGS='-ljemalloc' -DWITH_SAFEMALLOC=OFF"
        MariaDBMAOpt=''
        NginxMAOpt="--with-ld-opt='-ljemalloc'"
     elif [ "${SelectMalloc}" =  "3" ]; then
        MySQL51MAOpt='--with-mysqld-ldflags=-ltcmalloc'
        MySQL55MAOpt="-DCMAKE_EXE_LINKER_FLAGS='-ltcmalloc' -DWITH_SAFEMALLOC=OFF"
        MariaDBMAOpt="-DCMAKE_EXE_LINKER_FLAGS='-ltcmalloc' -DWITH_SAFEMALLOC=OFF"
        NginxMAOpt='--with-google_perftools_module'
    fi
}

Nginx_Selection()
{
    #which Nginx Version do you want to install?
    echo "==========================="
    NginxSelect="1"
    Echo_Yellow "You have 4 options for your Nginx install."
    echo -e "   \e[0;31m1\e[0m: Install Nginx ${Nginx_Version1} \e[44;37m(Default)\e[0m"
    echo -e "   \e[0;31m2\e[0m: Install Nginx ${Nginx_Version2}"
    echo -e "   \e[0;31m3\e[0m: Install Nginx ${Nginx_Version1} with Memc Module"
    echo -e "   \e[0;31m4\e[0m: Install Nginx ${Nginx_Version2} with Memc Module"
    echo "Notice: Select Nginx with Memc Module will install memcached!"
    read -p "Enter your choice (1, 2, 3 or 4): " NginxSelect

    if [ "${NginxSelect}" = "1" ]; then
        echo -e " \e[0;32mYou will install \e[0;31mNginx ${Nginx_Version1}\e[0m"
     elif [ "${NginxSelect}" = "2" ]; then
        echo -e " \e[0;32mYou will install \e[0;31mNginx ${Nginx_Version2}\e[0m"
     elif [ "${NginxSelect}" = "3" ]; then
        echo -e " \e[0;32mYou will install \e[0;31mNginx ${Nginx_Version1} with Memc Module\e[0m"
     elif [ "${NginxSelect}" = "4" ]; then
        echo -e " \e[0;32mYou will install \e[0;31mNginx ${Nginx_Version2} with Memc Module\e[0m"
     else
        echo -e " \e[0;32mNo input,You will install \e[0;31mNginx ${Nginx_Version1}\e[0m"
        NginxSelect="1"
    fi
}

Apache_Selection()
{
    echo "==========================="
#set Server Administrator Email Address
    ServerAdmin="hongm@sina.com"
    echo -e " \e[0;33mPlease setup Administrator Email Address for Apache.(Default: ${ServerAdmin})\e[0m"
    read -p "Please enter:" ServerAdmin
    if [ "${ServerAdmin}" == "" ]; then
        ServerAdmin="hongm@sina.com"
    fi
    echo -e " \e[0;32mAdministrator Email Address will set to \e[0;31m${ServerAdmin}\e[0m!"

#which Apache Version do you want to install?
    echo "==========================="
    ApacheSelect="2"
    Echo_Yellow "You have 2 options for your Apache install."
    echo -e "   \e[0;31m1\e[0m: Install Apache ${Apache_Version1}"
    echo -e "   \e[0;31m2\e[0m: Install Apache ${Apache_Version2} \e[44;37m(Default)\e[0m"
    read -p "Enter your choice (1 or 2): " ApacheSelect

    if [ "${ApacheSelect}" = "1" ]; then
        echo -e " \e[0;32mYou will install \e[0;31mApache ${Apache_Version1}\e[0m"
     elif [ "${ApacheSelect}" = "2" ]; then
        echo -e " \e[0;32mYou will install \e[0;31mApache ${Apache_Version2}\e[0m"
     else
        echo -e " \e[0;32mNo input,You will install \e[0;31mApache ${Apache_Version2}\e[0m"
        ApacheSelect="2"
    fi
}

Press_Install()
{
    echo ""
    echo "Press any key to install...or Press Ctrl+c to cancel"
    OLDCONFIG=`stty -g`
    stty -icanon -echo min 1 time 0
    dd count=1 2>/dev/null
    stty ${OLDCONFIG}
    . include/version.sh
    if ps aux | grep "yum" | grep -qv "grep"; then
        killall yum
     elif ps aux | grep "apt-get" | grep -qv "grep"; then
        killall apt-get
    fi
}

Press_Start()
{
    echo ""
    echo "Press any key to start...or Press Ctrl+c to cancel"
    OLDCONFIG=`stty -g`
    stty -icanon -echo min 1 time 0
    dd count=1 2>/dev/null
    stty ${OLDCONFIG}
}

Install_LSB()
{
    if [ "$PM" = "yum" ]; then
        yum -y install redhat-lsb
     elif [ "$PM" = "apt" ]; then
        apt-get update
        apt-get install -y lsb-release
    fi
}

Get_Dist_Version()
{
    Install_LSB
    eval ${DISTRO}_Version=`lsb_release -rs`
    eval echo "${DISTRO} \${${DISTRO}_Version}"
}

Get_Dist_Name()
{
    if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
        DISTRO='CentOS'
        PM='yum'
     elif grep -Eqi "Red Hat Enterprise Linux Server" /etc/issue || grep -Eq "Red Hat Enterprise Linux Server" /etc/*-release; then
        DISTRO='RHEL'
        PM='yum'
     elif grep -Eqi "Aliyun" /etc/issue || grep -Eq "Aliyun" /etc/*-release; then
        DISTRO='Aliyun'
        PM='yum'
     elif grep -Eqi "Fedora" /etc/issue || grep -Eq "Fedora" /etc/*-release; then
        DISTRO='Fedora'
        PM='yum'
     elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then
        DISTRO='Debian'
        PM='apt'
     elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then
        DISTRO='Ubuntu'
        PM='apt'
     elif grep -Eqi "Raspbian" /etc/issue || grep -Eq "Raspbian" /etc/*-release; then
        DISTRO='Raspbian'
        PM='apt'
     else
        DISTRO='unknow'
    fi
    Get_OS_Bit
}

Get_CentOS_Version()
{
    Get_Dist_Name
    if grep -Eqi "release 5." /etc/redhat-release; then
        CentOS_Ver='5'
      elif grep -Eqi "release 6." /etc/redhat-release; then
        CentOS_Ver='6'
      elif grep -Eqi "release 7." /etc/redhat-release; then
        CentOS_Ver='7'
        firewall-cmd --zone=public --add-port=80/tcp --permanent
        firewall-cmd --reload
    fi
}

Get_RHEL_Version()
{
    Get_Dist_Name
    if [ "${DISTRO}" = "RHEL" ]; then
        if grep -Eqi "release 5." /etc/redhat-release; then
            echo "Current Version: RHEL Ver 5"
            RHEL_Ver='5'
        elif grep -Eqi "release 6." /etc/redhat-release; then
            echo "Current Version: RHEL Ver 6"
            RHEL_Ver='6'
        elif grep -Eqi "release 7." /etc/redhat-release; then
            echo "Current Version: RHEL Ver 7"
            RHEL_Ver='7'
            firewall-cmd --zone=public --add-port=80/tcp --permanent
            firewall-cmd --reload
        fi
    fi
}

Get_OS_Bit()
{
    if [[ `getconf WORD_BIT` = '32' && `getconf LONG_BIT` = '64' ]] ; then
        Is_64bit='y'
     else
        Is_64bit='n'
    fi
}

Get_ARM()
{
    if uname -m | grep -Eqi "arm"; then
        Is_ARM='y'
    fi
}

Tar_Cd()
{
    local FileName=$1
    local DirName=$2
    cd ${cur_dir}/src
    [[ -d "${DirName}" ]] && rm -rf ${DirName}
    echo "Uncompress ${FileName}..."
    tar zxf ${FileName}
    echo "cd ${DirName}..."
    cd ${DirName}
}

Print_APP_Ver()
{
    echo ""
    Echo_Green "You will install ${Stack} stack."
    if [ "${Stack}" != "lamp" ] && [ "${NginxSelect}" != "" ]; then
        echo "    ${Nginx_Ver}"
    fi

    if [ "${DBSelect}" != "" ]; then
        if [ ${DBSelect} -ge 1 ] && [ ${DBSelect} -lt 6 ]; then
            echo "    ${Mysql_Ver}"
         elif [ ${DBSelect} -ge 6 ] || [ ${DBSelect} -ne 0 ]; then
            echo "    ${Mariadb_Ver}"
        fi
    fi

    if [ "${PHPSelect}" != "" ]; then
        echo "    ${Php_Ver}"
    fi

    if [ "${Stack}" != "lnmp" ] && [ "${ApacheSelect}" != "" ]; then
        echo "    ${Apache_Version}"
    fi

    if [ "${SelectMalloc}" = "2" ]; then
        echo "    ${Jemalloc_Ver}"
     elif [ "${SelectMalloc}" = "3" ]; then
        echo "    ${TCMalloc_Ver}"
    fi

    echo "    Enable InnoDB: ${InstallInnodb}"
    echo "Print lnmp.conf infomation..."
    echo "    Download Mirror: ${Download_Mirror}"
    echo "    Nginx Additional Modules: ${Nginx_Modules_Options}"
    echo "    PHP Additional Modules: ${PHP_Modules_Options}"
    echo "    Database Directory: ${DB_Data_Dir}"
    echo "    Default Website Directory: ${Default_Website_Dir}"
    sleep 5
}

Print_Sys_Info()
{
    cat /etc/issue
    cat /etc/*-release
    uname -a
    MemTotal=`free -m | grep Mem | awk '{print  $2}'`  
    echo "Memory is: ${MemTotal} MB "
    df -h
}

StartUp()
{
    init_name=$1
    echo "Add ${init_name} service at system startup..."
    if [ "$PM" = "yum" ]; then
        chkconfig --add ${init_name}
        chkconfig ${init_name} on
     elif [ "$PM" = "apt" ]; then
        update-rc.d -f ${init_name} defaults
    fi
}

Remove_StartUp()
{
    init_name=$1
    echo "Removing ${init_name} service at system startup..."
    if [ "$PM" = "yum" ]; then
        chkconfig ${init_name} off
        chkconfig --del ${init_name}
     elif [ "$PM" = "apt" ]; then
        update-rc.d -f ${init_name} remove
    fi
}

Color_Text()
{
    echo -e " \e[0;$2m$1\e[0m"
}

Echo_Red()
{
    echo $(Color_Text "$1" "31")
}

Echo_Green()
{
    echo $(Color_Text "$1" "32")
}

Echo_Yellow()
{
    echo $(Color_Text "$1" "33")
}

Echo_Blue()
{
    echo $(Color_Text "$1" "34")
}

Get_PHP_Ext_Dir()
{
    Cur_PHP_Version="`/usr/local/php/bin/php-config --version`"
    zend_ext_dir="`/usr/local/php/bin/php-config --extension-dir`/"
}

Check_Stack()
{
    if [[ -s /usr/local/php/bin/php-cgi || -s /usr/local/php/sbin/php-fpm ]] && [[ -s /usr/local/php/etc/php-fpm.conf && -s /etc/init.d/php-fpm && -s /usr/local/nginx/sbin/nginx ]]; then
        Get_Stack="lnmp"
     elif [[ -s /usr/local/nginx/sbin/nginx && -s /usr/local/apache/bin/httpd && -s /usr/local/apache/conf/httpd.conf && -s /etc/init.d/httpd && ! -s /usr/local/php/sbin/php-fpm ]]; then
        Get_Stack="lnmpa"
     elif [[ -s /usr/local/apache/bin/httpd && -s /usr/local/apache/conf/httpd.conf && -s /etc/init.d/httpd && ! -s /usr/local/php/sbin/php-fpm ]]; then
        Get_Stack="lamp"
     else
        Get_Stack="unknow"
    fi
}

Check_DB()
{
    if [[ -s /usr/local/mariadb/bin/mysql && -s /usr/local/mariadb/bin/mysqld_safe && -s /etc/my.cnf ]]; then
        MySQL_Bin="/usr/local/mariadb/bin/mysql"
        MySQL_Config="/usr/local/mariadb/bin/mysql_config"
        MySQL_Dir="/usr/local/mariadb"
        Is_MySQL="n"
        DB_Name="mariadb"
     else
        MySQL_Bin="/usr/local/mysql/bin/mysql"
        MySQL_Config="/usr/local/mysql/bin/mysql_config"
        MySQL_Dir="/usr/local/mysql"
        Is_MySQL="y"
        DB_Name="mysql"
    fi
}

Verify_DB_Password()
{
    Check_DB
    status=1
    while [ $status -eq 1 ]; do
        stty -echo
        echo "Enter current root password of Database (Password will not shown): "
        read DB_Root_Password
        echo
        stty echo
        Make_TempMycnf "${DB_Root_Password}"
        Do_Query ""
        status=$?
    done
    echo "OK, MySQL root password correct."
}

Do_Query()
{
    echo "$1" >/tmp/.mysql.tmp
    Check_DB
    ${MySQL_Bin} --defaults-file=~/.my.cnf </tmp/.mysql.tmp
    return $?
}

Make_TempMycnf()
{
    cat >~/.my.cnf<<EOF
[client]
user=root
password='$1'
EOF
}

Check_Hosts()
{
    if grep -Eqi '^127.0.0.1[[:space:]]*localhost' /etc/hosts; then
        echo "Hosts: ok."
     else
        echo "127.0.0.1 localhost.localdomain localhost" >> /etc/hosts
    fi
}


Set_Timezone()
{
    Echo_Blue "Setting timezone..."
    rm -rf /etc/localtime
    ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
}

CentOS_InstallNTP()
{
    Echo_Blue "[+] Installing ntp..."
    yum install -y ntp
    ntpdate -u pool.ntp.org
    date
}

Deb_InstallNTP()
{
    apt-get update -y
    Echo_Blue "[+] Installing ntp..."
    apt-get install -y ntpdate
    ntpdate -u pool.ntp.org
    date
}

CentOS_RemoveAMP()
{
    Echo_Blue "[-] Yum remove packages..."
    rpm -qa|grep httpd
    rpm -e httpd httpd-tools
    rpm -qa|grep mysql
    rpm -e mysql mysql-libs
    rpm -qa|grep php
    rpm -e php-mysql php-cli php-gd php-common php
    yum -y remove httpd*
    yum -y remove mysql-server mysql mysql-libs
    yum -y remove php*
    yum clean all
}

Deb_RemoveAMP()
{
    Echo_Blue "[-] apt-get remove packages..."
    apt-get update -y
    for removepackages in apache2 apache2-doc apache2-utils apache2.2-common apache2.2-bin apache2-mpm-prefork apache2-doc apache2-mpm-worker mysql-client mysql-server mysql-common mysql-server-core-5.5 mysql-client-5.5 php5 php5-common php5-cgi php5-cli php5-mysql php5-curl php5-gd;
        do apt-get purge -y $removepackages; done
    killall apache2
    dpkg -l |grep apache 
    dpkg -P apache2 apache2-doc apache2-mpm-prefork apache2-utils apache2.2-common
    dpkg -l |grep mysql 
    dpkg -P mysql-server mysql-common libmysqlclient15off libmysqlclient15-dev
    dpkg -l |grep php 
    dpkg -P php5 php5-common php5-cli php5-cgi php5-mysql php5-curl php5-gd
    apt-get autoremove -y && apt-get clean
}

Disable_Selinux()
{
    if [ -s /etc/selinux/config ]; then
        sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
    fi
}

Xen_Hwcap_Setting()
{
    if [ -s /etc/ld.so.conf.d/libc6-xen.conf ]; then
        sed -i 's/hwcap 1 nosegneg/hwcap 0 nosegneg/g' /etc/ld.so.conf.d/libc6-xen.conf
    fi
}

RHEL_Modify_Source()
{
    Get_RHEL_Version
    \cp ${cur_dir}/conf/CentOS-Base-ustc.repo /etc/yum.repos.d/CentOS-Base.repo
    sed -i "s/\$releasever/${RHEL_Ver}/g" /etc/yum.repos.d/CentOS-Base.repo
    sed -i "s/RPM-GPG-KEY-CentOS-7/RPM-GPG-KEY-CentOS-${RHEL_Ver}/g" /etc/yum.repos.d/CentOS-Base.repo
    yum clean all
    yum makecache
}

CentOS_Modify_Source()
{
    Get_CentOS_Version
    \cp ${cur_dir}/conf/CentOS-Base-ustc.repo /etc/yum.repos.d/CentOS-Base.repo
    sed -i "s/\$releasever/${CentOS_Ver}/g" /etc/yum.repos.d/CentOS-Base.repo
    sed -i "s/RPM-GPG-KEY-CentOS-7/RPM-GPG-KEY-CentOS-${CentOS_Ver}/g" /etc/yum.repos.d/CentOS-Base.repo
    yum clean all
    yum makecache
}

Debian_Modify_Source()
{
    CodeName=''
    if grep -Eqi "6." /etc/debian_version ; then
        CodeName='squeeze'
     elif grep -Eqi "7." /etc/debian_version ; then
        CodeName='wheezy'
     elif grep -Eqi "8." /etc/debian_version ; then
        CodeName='jessie'
     elif grep -Eqi "9." /etc/debian_version ; then
        CodeName='stretch'
     elif grep -Eqi "10." /etc/debian_version ; then
        CodeName='buster'
    fi
    if [ "${CodeName}" != "" ]; then
        \cp /etc/apt/sources.list /etc/apt/sources.list.$(date +"%Y%m%d")
        cat > /etc/apt/sources.list<<EOF
deb http://mirrors.ustc.edu.cn/debian/ ${CodeName} main contrib non-free
deb http://mirrors.ustc.edu.cn/debian/ ${CodeName}-updates main contrib non-free
deb http://mirrors.ustc.edu.cn/debian-security/ ${CodeName}/updates main contrib non-free
EOF
    fi
}

Ubuntu_Modify_Source()
{
    CodeName=''
    if grep -Eqi "10.04" /etc/*-release || echo "${Ubuntu_Version}" | grep -Eqi '^10.04'; then
        CodeName='lucid'
     elif grep -Eqi "10.10" /etc/*-release || echo "${Ubuntu_Version}" | grep -Eqi '^10.10'; then
        CodeName='maverick'
     elif grep -Eqi "11.04" /etc/*-release || echo "${Ubuntu_Version}" | grep -Eqi '^11.04'; then
        CodeName='natty'
     elif  grep -Eqi "11.10" /etc/*-release || echo "${Ubuntu_Version}" | grep -Eqi '^11.10'; then
        CodeName='oneiric'
     elif grep -Eqi "12.04" /etc/*-release || echo "${Ubuntu_Version}" | grep -Eqi '^12.04'; then
        Ubuntu_Deadline precise
     elif grep -Eqi "12.10" /etc/*-release || echo "${Ubuntu_Version}" | grep -Eqi '^12.10'; then
        CodeName='quantal'
     elif grep -Eqi "13.04" /etc/*-release || echo "${Ubuntu_Version}" | grep -Eqi '^13.04'; then
        CodeName='raring'
     elif grep -Eqi "13.10" /etc/*-release || echo "${Ubuntu_Version}" | grep -Eqi '^13.10'; then
        CodeName='saucy'
     elif grep -Eqi "14.04" /etc/*-release || echo "${Ubuntu_Version}" | grep -Eqi '^14.04'; then
        Ubuntu_Deadline trusty
     elif grep -Eqi "14.10" /etc/*-release || echo "${Ubuntu_Version}" | grep -Eqi '^14.10'; then
        CodeName='utopic'
     elif grep -Eqi "15.04" /etc/*-release || echo "${Ubuntu_Version}" | grep -Eqi '^15.04'; then
        Ubuntu_Deadline vivid
     elif grep -Eqi "15.10" /etc/*-release || echo "${Ubuntu_Version}" | grep -Eqi '^15.10'; then
        Ubuntu_Deadline wily
     elif grep -Eqi "16.04" /etc/*-release || echo "${Ubuntu_Version}" | grep -Eqi '^16.04'; then
        Ubuntu_Deadline xenial
     elif grep -Eqi "16.10" /etc/*-release || echo "${Ubuntu_Version}" | grep -Eqi '^16.10'; then
        Ubuntu_Deadline yakkety
     elif grep -Eqi "17.04" /etc/*-release || echo "${Ubuntu_Version}" | grep -Eqi '^17.04'; then
        Ubuntu_Deadline zesty
    fi
    if [ "${CodeName}" != "" ]; then
        \cp /etc/apt/sources.list /etc/apt/sources.list.$(date +"%Y%m%d")
        cat > /etc/apt/sources.list<<EOF
deb http://old-releases.ubuntu.com/ubuntu/ ${CodeName} main restricted universe multiverse
deb http://old-releases.ubuntu.com/ubuntu/ ${CodeName}-security main restricted universe multiverse
deb http://old-releases.ubuntu.com/ubuntu/ ${CodeName}-updates main restricted universe multiverse
deb http://old-releases.ubuntu.com/ubuntu/ ${CodeName}-proposed main restricted universe multiverse
deb http://old-releases.ubuntu.com/ubuntu/ ${CodeName}-backports main restricted universe multiverse
deb-src http://old-releases.ubuntu.com/ubuntu/ ${CodeName} main restricted universe multiverse
deb-src http://old-releases.ubuntu.com/ubuntu/ ${CodeName}-security main restricted universe multiverse
deb-src http://old-releases.ubuntu.com/ubuntu/ ${CodeName}-updates main restricted universe multiverse
deb-src http://old-releases.ubuntu.com/ubuntu/ ${CodeName}-proposed main restricted universe multiverse
deb-src http://old-releases.ubuntu.com/ubuntu/ ${CodeName}-backports main restricted universe multiverse
EOF
    fi
}

Check_Old_Releases_URL()
{
    OR_Status=`wget --spider --server-response http://old-releases.ubuntu.com/ubuntu/dists/$1/Release 2>&1 | awk '/^  HTTP/{print $2}'`
    if [ ${OR_Status} != "404" ]; then
        echo "Ubuntu old-releases status: ${OR_Status}";
        CodeName=$1
    fi
}

Ubuntu_Deadline()
{
    precise_deadline=`date -d "2017-5-27 00:00:00" +%s`
    trusty_deadline=`date -d "2019-4-4 00:00:00" +%s`
    vivid_deadline=`date -d "2016-2-24 00:00:00" +%s`
    wily_deadline=`date -d "2016-7-22 00:00:00" +%s`
    xenial_deadline=`date -d "2021-4-21 00:00:00" +%s`
    yakkety_deadline=`date -d "2017-7-28 00:00:00" +%s`
    zesty_deadline=`date -d "2018-1-31 00:00:00" +%s`
    cur_time=`date  +%s`
    case "$1" in
        precise)
            if [ ${cur_time} -gt ${precise_deadline} ]; then
                echo "${cur_time} > ${precise_deadline}"
                Check_Old_Releases_URL precise
            fi
            ;;
        trusty)
            if [ ${cur_time} -gt ${utopic_deadline} ]; then
                echo "${cur_time} > ${utopic_deadline}"
                Check_Old_Releases_URL utopic
            fi
            ;;
        vivid)
            if [ ${cur_time} -gt ${vivid_deadline} ]; then
                echo "${cur_time} > ${vivid_deadline}"
                Check_Old_Releases_URL vivid
            fi
            ;;
        wily)
            if [ ${cur_time} -gt ${wily_deadline} ]; then
                echo "${cur_time} > ${wily_deadline}"
                Check_Old_Releases_URL wily
            fi
            ;;
        xenial)
            if [ ${cur_time} -gt ${wily_deadline} ]; then
                echo "${cur_time} > ${wily_deadline}"
                Check_Old_Releases_URL wily
            fi
            ;;
        yakkety)
            if [ ${cur_time} -gt ${wily_deadline} ]; then
                echo "${cur_time} > ${wily_deadline}"
                Check_Old_Releases_URL wily
            fi
            ;;
        zesty)
            if [ ${cur_time} -gt ${zesty_deadline} ]; then
                echo "${cur_time} > ${zesty_deadline}"
                Check_Old_Releases_URL zesty
            fi
            ;;
    esac
}

CentOS_Dependent()
{
    cp /etc/yum.conf /etc/yum.conf.lnmp
    sed -i 's:exclude=.*:exclude=:g' /etc/yum.conf
    Echo_Blue "[+] Yum installing dependent packages..."
    for packages in make cmake gcc gcc-c++ gcc-g77 flex bison file libtool libtool-libs autoconf kernel-devel patch sudo wget crontabs libjpeg libjpeg-devel libpng libpng-devel libpng10 libpng10-devel gd gd-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel unzip tar bzip2 bzip2-devel libevent libevent-devel ncurses ncurses-devel curl curl-devel libcurl libcurl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel vim-minimal gettext gettext-devel ncurses-devel gmp-devel pspell-devel unzip libcap diffutils ca-certificates net-tools libc-client-devel psmisc libXpm-devel git-core c-ares-devel libicu-devel libxslt libxslt-devel xz;
        do yum -y install $packages; done
    mv -f /etc/yum.conf.lnmp /etc/yum.conf
}

Deb_Dependent()
{
    Echo_Blue "[+] Apt-get installing dependent packages..."
    apt-get update -y
    apt-get autoremove -y
    apt-get -fy install
    export DEBIAN_FRONTEND=noninteractive
    apt-get install -y build-essential gcc g++ make
    for packages in debian-keyring debian-archive-keyring build-essential gcc g++ make cmake autoconf automake re2c sudo wget cron bzip2 libzip-dev libc6-dev bison file rcconf flex vim bison m4 gawk less cpp binutils diffutils unzip tar bzip2 libbz2-dev libncurses5 libncurses5-dev libtool libevent-dev openssl libssl-dev zlibc libsasl2-dev libltdl3-dev libltdl-dev zlib1g zlib1g-dev libbz2-1.0 libbz2-dev libglib2.0-0 libglib2.0-dev libpng3 libjpeg62 libjpeg62-dev libjpeg-dev libpng-dev libpng12-0 libpng12-dev libkrb5-dev curl libcurl3 libcurl3-gnutls libcurl4-gnutls-dev libcurl4-openssl-dev libpq-dev libpq5 gettext libjpeg-dev libpng12-dev libxml2-dev libcap-dev ca-certificates libc-client2007e-dev psmisc patch git libc-ares-dev libicu-dev e2fsprogs libxslt libxslt1-dev libc-client-dev xz-utils;
        do apt-get --no-install-recommends install -y $packages --force-yes; done
}

Check_Mirror()
{
    if [ "$PM" = "yum" ]; then
        yum install -y curl
    elif [ "$PM" = "apt" ]; then
        apt-get update
        apt-get install -y curl
    fi
    if [ "${Download_Mirror}" = "http://hongm.emoodle.org/down" ]; then
        mirror_code=`curl -o /dev/null -m 20 --connect-timeout 20 -sk -w %{http_code} http://hongm.emoodle.org/down/`
        if [ "${mirror_code}" != "200" ]; then
            mirror_code=`curl -o /dev/null -m 20 --connect-timeout 20 -sk -w %{http_code} https://study.wjszzx.com.cn/soft/`
            if [ "${mirror_code}" = "200" ]; then
                echo "Change to mirror https://study.wjszzx.com.cn/soft"
                Download_Mirror='https://study.wjszzx.com.cn/soft'
            else
                echo "Dwonload Server Error!"
                exit 1
            fi
        fi
    fi
}

Download_Files()
{
    local URL=$1
    local FileName=$2
    if [ -s "${FileName}" ]; then
        echo "${FileName} [found]"
     else
        echo "Error: ${FileName} not found!!!download now..."
        wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate ${URL}
    fi
}

Check_Download()
{
    Echo_Blue "[+] Downloading files..."
    mkdir -p ${cur_dir}/src
    cd ${cur_dir}/src
    Download_Files ${Download_Mirror}/nginx/${Nginx_Ver}.tar.gz ${Nginx_Ver}.tar.gz
        [[ ! -f ${Nginx_Ver}.tar.gz ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate http://nginx.org/download/${Nginx_Ver}.tar.gz
    if [ ${DBSelect} -ge 6 ]; then
        Download_Files ${Download_Mirror}/mariadb/${Mariadb_Ver}.tar.gz ${Mariadb_Ver}.tar.gz
            [[ ! -f ${Mariadb_Ver}.tar.gz ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate http://mirrors.ustc.edu.cn/mariadb/${MariaDB_Ver}/source/${MariaDB_Ver}.tar.gz --no-check-certificate
     elif  [ "${DBSelect}" = "5" ]; then
        Download_Files ${Download_Mirror}/mysql/mysql-boost-$( echo ${Mysql_Ver} | awk -F"-" '{print $2}').tar.gz mysql-boost-$( echo ${Mysql_Ver} | awk -F"-" '{print $2}').tar.gz
            [[ ! -f ${Mysql_Ver}.tar.gz ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate http://cdn.mysql.com/Downloads/MySQL-$(echo ${Mysql_Ver} | awk -F"-" '{print $2}'|cut -d. -f1-2)/mysql-boost-$( echo ${Mysql_Ver} | awk -F"-" '{print $2}').tar.gz
     else
        Download_Files ${Download_Mirror}/mysql/${Mysql_Ver}.tar.gz ${Mysql_Ver}.tar.gz
            [[ ! -f ${Mysql_Ver}.tar.gz ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate http://cdn.mysql.com/Downloads/MySQL-$(echo ${Mysql_Ver} | awk -F"-" '{print $2}'|cut -d. -f1-2)/${Mysql_Ver}.tar.gz
    fi
    Download_Files ${Download_Mirror}/php/${Php_Ver}.tar.bz2 ${Php_Ver}.tar.bz2
    [[ ! -f ${Php_Ver}.tar.bz2 ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate http://cn2.php.net/distributions/${PHP_Ver}.tar.bz2
    if [ ${PHPSelect} = "1" ]; then
        Download_Files ${Download_Mirror}/phpfpm/${Php_Ver}-fpm-0.5.14.diff.gz ${Php_Ver}-fpm-0.5.14.diff.gz
            [[ ! -f ${Php_Ver}-fpm-0.5.14.diff.gz ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate https://php-fpm.org/downloads/${Php_Ver}-fpm-0.5.14.diff.gz
    fi
    Download_Files ${Download_Mirror}/phpmyadmin/${PhpMyAdmin_Ver}.tar.gz ${PhpMyAdmin_Ver}.tar.gz
    [[ ! -f ${PhpMyAdmin_Ver}.tar.gz ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate https://files.phpmyadmin.net/phpMyAdmin/$(echo ${PhpMyAdmin_Ver} | awk -F"-" '{print $2}')/${PhpMyAdmin_Ver}.tar.gz
    if [ "${Stack}" != "lnmp" ]; then
        Download_Files ${Download_Mirror}/apache/${Apache_Version}.tar.gz ${Apache_Version}.tar.gz
            [[ ! -f ${Apache_Version}.tar.gz ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/${Apache_Version}.tar.gz
        Download_Files ${Download_Mirror}/apache/${APR_Ver}.tar.gz ${APR_Ver}.tar.gz
            [[ ! -f ${APR_Ver}.tar.gz ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate https://mirrors.tuna.tsinghua.edu.cn/apache/apr/${APR_Ver}.tar.gz
        Download_Files ${Download_Mirror}/apache/${APR_Util_Ver}.tar.gz ${APR_Util_Ver}.tar.gz
            [[ ! -f ${APR_Util_Ver}.tar.gz ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate https://mirrors.tuna.tsinghua.edu.cn/apache/apr/${APR_Util_Ver}.tar.gz        Download_Files ${Download_Mirror}/apache/rpaf/${Mod_RPAF_Ver}.tar.gz ${Mod_RPAF_Ver}.tar.gz
        Download_Files ${Download_Mirror}/apache/rpaf/${Mod_RPAF_Ver}.tar.gz ${Mod_RPAF_Ver}.tar.gz
            #[[ ! -f ${Mod_RPAF_Ver}.tar.gz ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate 
    fi
    if [ "${Is_64bit}" = "y" ] ; then
        if [ "${PHPSelect}" = "1" ]; then
            Download_Files ${Download_Mirror}/zend/ZendOptimizer-3.3.9-linux-glibc23-x86_64.tar.gz ZendOptimizer-3.3.9-linux-glibc23-x86_64.tar.gz
         elif [ "${PHPSelect}" = "2" ]; then
            Download_Files ${Download_Mirror}/zend/ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz
         elif [ "${PHPSelect}" = "3" ]; then
            Download_Files ${Download_Mirror}/zend/ZendGuardLoader-70429-PHP-5.4-linux-glibc23-x86_64.tar.gz ZendGuardLoader-70429-PHP-5.4-linux-glibc23-x86_64.tar.gz
         elif [ "${PHPSelect}" = "4" ]; then
            Download_Files ${Download_Mirror}/zend/zend-loader-php5.5-linux-x86_64.tar.gz zend/zend-loader-php5.5-linux-x86_64.tar.gz
         elif [ "${PHPSelect}" = "5" ]; then
            Download_Files ${Download_Mirror}/zend/zend-loader-php5.6-linux-x86_64.tar.gz zend-loader-php5.6-linux-x86_64.tar.gz
        fi
    else
        if [ "${PHPSelect}" = "1" ]; then
            Download_Files ${Download_Mirror}/zend/ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz
         elif [ "${PHPSelect}" = "2" ]; then
            Download_Files ${Download_Mirror}/zend/ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz
         elif [ "${PHPSelect}" = "3" ]; then
            Download_Files ${Download_Mirror}/zend/ZendGuardLoader-70429-PHP-5.4-linux-glibc23-i386.tar.gz ZendGuardLoader-70429-PHP-5.4-linux-glibc23-i386.tar.gz
         elif [ "${PHPSelect}" = "4" ]; then
            Download_Files ${Download_Mirror}/zend/zend-loader-php5.5-linux-i386.tar.gz zend-loader-php5.5-linux-i386.tar.gz
         elif [ "${PHPSelect}" = "5" ]; then
            Download_Files ${Download_Mirror}/zend/zend-loader-php5.6-linux-i386.tar.gz zend-loader-php5.6-linux-i386.tar.gz
        fi
    fi
    Download_Files ${Download_Mirror}/autoconf/${Autoconf_Ver}.tar.gz ${Autoconf_Ver}.tar.gz
        [[ ! -s ${Autoconf_Ver}.tar.gz ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate https://ftp.gnu.org/gnu/autoconf/${Autoconf_Ver}.tar.gz
    Download_Files ${Download_Mirror}/libiconv/${Libiconv_Ver}.tar.gz ${Libiconv_Ver}.tar.gz
        [[ ! -s ${Libiconv_Ver}.tar.gz ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate http://ftp.gnu.org/gnu/libiconv/${Libiconv_Ver}.tar.gz
    Download_Files ${Download_Mirror}/libmcrypt/${LibMcrypt_Ver}.tar.gz ${LibMcrypt_Ver}.tar.gz
    Download_Files ${Download_Mirror}/mcrypt/${Mcypt_Ver}.tar.gz ${Mcypt_Ver}.tar.gz
    Download_Files  ${Download_Mirror}/cmake/${Cmake_Ver}.tar.gz ${Cmake_Ver}.tar.gz
        [[ ! -s ${Cmake_Ver}.tar.gz ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate https://cmake.org/files/v$(echo ${Cmake_Ver} | awk -F[-.] '{print $2}').$(echo ${Cmake_Ver} | awk -F[-.] '{print $3}')/${Cmake_Ver}.tar.gz --no-check-certificate
    Download_Files ${Download_Mirror}/mhash/${Mhash_Ver}.tar.bz2 ${Mhash_Ver}.tar.bz2
        [[ ! -s ${Mhash_Ver}.tar.bz2 ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate https://sourceforge.net/projects/mhash/files/mhash/$(echo ${Mhash_Ver} | awk -F"-" '{print $2}')/${Mhash_Ver}.tar.bz2
    Download_Files ${Download_Mirror}/freetype/${Freetype_Ver}.tar.bz2 ${Freetype_Ver}.tar.bz2
        [[ ! -f ${Freetype_Ver}.tar.bz2 ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate http://download.savannah.gnu.org/releases/freetype/${Freetype_Ver}.tar.bz2
    Download_Files ${Download_Mirror}/curl/${Curl_Ver}.tar.bz2 ${Curl_Ver}.tar.bz2
        [[ ! -s ${Curl_Ver}.tar.bz2 ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate https://curl.haxx.se/download/${Curl_Ver}.tar.bz2
    Download_Files ${Download_Mirror}/pcre/${Pcre_Ver}.tar.bz2 ${Pcre_Ver}.tar.bz2
        [[ ! -s ${Pcre_Ver}.tar.bz2 ]] && wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/${Pcre_Ver}.tar.bz2
}

Install_Autoconf()
{
    Echo_Blue "[+] Installing ${Autoconf_Ver}"
    cd ${cur_dir}/src
    Download_Files ${Download_Mirror}/autoconf/${Autoconf_Ver}.tar.gz ${Autoconf_Ver}.tar.gz
    Tar_Cd ${Autoconf_Ver}.tar.gz ${Autoconf_Ver}
    ./configure --prefix=/usr/local/${Autoconf_Ver}
    make && make install
    cd ${cur_dir}/src/
    rm -rf ${Autoconf_Ver}
    export PHP_AUTOCONF=/usr/local/${Autoconf_Ver}/bin/autoconf
    export PHP_AUTOHEADER=/usr/local/${Autoconf_Ver}/bin/autoheader
}

Install_Libiconv()
{
    Echo_Blue "[+] Installing ${Libiconv_Ver}"
    cd ${cur_dir}/src
    Download_Files ${Download_Mirror}/libiconv/${Libiconv_Ver}.tar.gz ${Libiconv_Ver}.tar.gz
    Tar_Cd ${Libiconv_Ver}.tar.gz ${Libiconv_Ver}
    patch -p0 < ${cur_dir}/src/patch/libiconv-glibc-2.16.patch
    ./configure --enable-static
    make && make install
    ldconfig
    cd ${cur_dir}/src/
    rm -rf ${Libiconv_Ver}
}

Install_Libmcrypt()
{
    Echo_Blue "[+] Installing ${LibMcrypt_Ver}"
    cd ${cur_dir}/src
    Download_Files ${Download_Mirror}/libmcrypt/${LibMcrypt_Ver}.tar.gz ${LibMcrypt_Ver}.tar.gz
    Tar_Cd ${LibMcrypt_Ver}.tar.gz ${LibMcrypt_Ver}
    ./configure
    make && make install
    /sbin/ldconfig
    cd libltdl/
    ./configure --enable-ltdl-install
    make && make install
    ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
    ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
    ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
    ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
    ldconfig
    cd ${cur_dir}/src/
    rm -rf ${LibMcrypt_Ver}
}

Install_Libxsl()
{
    Echo_Blue "[+] Installing ${Libxsl_Ver}"
    cd ${cur_dir}/src
    Download_Files ${Download_Mirror}/libxsl/${Libxsl_Ver}.tar.gz ${Libxsl_Ver}.tar.gz
    Tar_Cd ${Libxsl_Ver}.tar.gz ${Libxsl_Ver}
    ./configure
    make && make install
    ldconfig
    cd ${cur_dir}/src/
    rm -rf ${Libxsl_Ver}
}

Install_Libtidy()
{
    Echo_Blue "[+] Installing ${Libtidy_Ver}"
    cd ${cur_dir}/src
    Download_Files ${Download_Mirror}/tidy/${Libtidy_Ver}.tar.gz ${Libtidy_Ver}.tar.gz
    Tar_Cd ${Libtidy_Ver}.tar.gz ${Libtidy_Ver}
    ./configure
    make && make install
    ldconfig
    cd ${cur_dir}/src/
    rm -rf ${Libtidy_Ver}
}

Install_Libzip()
{
    Echo_Blue "[+] Installing ${Libzip_Ver}"
    cd ${cur_dir}/src
    Download_Files ${Download_Mirror}/libzip/${Libzip_Ver}.tar.gz ${Libzip_Ver}.tar.gz
    Tar_Cd ${Libzip_Ver}.tar.gz ${Libzip_Ver}
    ./configure --prefix=/usr/local/libzip
    make && make install
    ldconfig
    cd ${cur_dir}/src/
    rm -rf ${Libzip_Ver}
}

Install_Mcrypt()
{
    Echo_Blue "[+] Installing ${Mcypt_Ver}"
    cd ${cur_dir}/src
    Download_Files ${Download_Mirror}/mcrypt/${Mcypt_Ver}.tar.gz ${Mcypt_Ver}.tar.gz
    Tar_Cd ${Mcypt_Ver}.tar.gz ${Mcypt_Ver}
    ./configure
    make && make install
    ldconfig
    cd ${cur_dir}/src/
    rm -rf ${Mcypt_Ver}
}

Install_Mhash()
{
    Echo_Blue "[+] Installing ${Mash_Ver}"
    cd ${cur_dir}/src
    Download_Files ${Download_Mirror}/mhash/${Mhash_Ver}.tar.bz2 ${Mhash_Ver}.tar.bz2
    tar jxf ${Mhash_Ver}.tar.bz2
    cd ${Mhash_Ver}
    ./configure
    make && make install
    ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
    ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
    ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
    ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
    ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
    ldconfig
    cd ${cur_dir}/src/
    rm -rf ${Mhash_Ver}
}

Install_Freetype()
{
    Echo_Blue "[+] Installing ${Freetype_Ver}"
    cd ${cur_dir}/src
    Download_Files ${Download_Mirror}/freetype/${Freetype_Ver}.tar.bz2 ${Freetype_Ver}.tar.bz2
    tar jxf ${Freetype_Ver}.tar.bz2
    cd ${Freetype_Ver}
    ./configure --prefix=/usr/local/freetype
    make && make install
    ldconfig
    cd ${cur_dir}/src/
    rm -rf ${Freetype_Ver}
}

Install_Curl()
{
    Echo_Blue "[+] Installing ${Curl_Ver}"
    cd ${cur_dir}/src
    Download_Files ${Download_Mirror}/curl/${Curl_Ver}.tar.bz2 ${Curl_Ver}.tar.bz2
    tar jxf ${Curl_Ver}.tar.bz2
    cd ${Curl_Ver}
    ./configure --prefix=/usr/local/curl --enable-ares --without-nss --with-ssl
    make && make install
    ldconfig
    cd ${cur_dir}/src/
    rm -rf ${Curl_Ver}
}

Install_Pcre()
{
    Cur_Pcre_Ver=`pcre-config --version`
    if echo "${Cur_Pcre_Ver}" | grep -vEqi '^8.';then
        Echo_Blue "[+] Installing ${Pcre_Ver}"
        cd ${cur_dir}/src
        Download_Files ${Download_Mirror}/pcre/${Pcre_Ver}.tar.bz2 ${Pcre_Ver}.tar.bz2
        tar jxf ${Pcre_Ver}.tar.bz2
        cd ${Pcre_Ver}
        ./configure
        make && make install
        ldconfig
        cd ${cur_dir}/src/
        rm -rf ${Pcre_Ver}
    fi
}

Install_Jemalloc()
{
    Echo_Blue "[+] Installing ${Jemalloc_Ver}"
    cd ${cur_dir}/src
    Download_Files ${Download_Mirror}/jemalloc/${Jemalloc_Ver}.tar.bz2 ${Jemalloc_Ver}.tar.bz2
    tar jxf ${Jemalloc_Ver}.tar.bz2
    cd ${Jemalloc_Ver}
    ./configure
    make && make install
    ldconfig
    cd ${cur_dir}/src/
    rm -rf ${Jemalloc_Ver}
}

Install_TCMalloc()
{
    Echo_Blue "[+] Installing ${TCMalloc_Ver}"
    if [ "${Is_64bit}" = "y" ] ; then
        cd ${cur_dir}/src
        Download_Files ${Download_Mirror}/libunwind/${Libunwind_Ver}.tar.gz ${Libunwind_Ver}.tar.gz
        Tar_Cd ${Libunwind_Ver}.tar.gz ${Libunwind_Ver}
        CFLAGS=-fPIC ./configure
        make CFLAGS=-fPIC
        make CFLAGS=-fPIC install
        cd ${cur_dir}/src/
        rm -rf ${Libunwind_Ver}
    fi
    cd ${cur_dir}/src
    Download_Files ${Download_Mirror}/tcmalloc/${TCMalloc_Ver}.tar.gz ${TCMalloc_Ver}.tar.gz
    Tar_Cd ${TCMalloc_Ver}.tar.gz ${TCMalloc_Ver}
    if [ "${Is_64bit}" = "y" ] ; then
        ./configure
     else
        ./configure --enable-frame-pointers
    fi
    make && make install
    ldconfig
    cd ${cur_dir}/src/
    rm -rf ${TCMalloc_Ver}
}

Install_Icu4c()
{
    if /usr/bin/icu-config --version | grep '^3.'; then
        Echo_Blue "[+] Installing ${Libicu4c_Ver}"
        cd ${cur_dir}/src
        Download_Files ${Download_Mirror}/icu4c/${Libicu4c_Ver}-src.tgz ${Libicu4c_Ver}-src.tgz
        Tar_Cd ${Libicu4c_Ver}-src.tgz icu/source
        ./configure
        make && make install
        ldconfig
        cd ${cur_dir}/src/
        rm -rf icu
    fi
}

Install_Boost()
{
    Echo_Blue "[+] Installing ${Boost_Ver}"
    cd ${cur_dir}/src
    Download_Files ${Download_Mirror}/boost/${Boost_Ver}.tar.bz2 ${Boost_Ver}.tar.bz2
    tar jxf ${Boost_Ver}.tar.bz2
    cd ${Boost_Ver}
    ./bootstrap.sh
    ./b2
    ./b2 install
    cd ${cur_dir}/src/
    rm -rf ${Boost_Ver}
}

Install_gcc()
{
    if [ "$PM" = "yum" ]; then
        gcc_version=$(rpm -qa|grep gcc|head -n 1|awk -F "-" '{print $2}')
     elif [ "$PM" = "apt" ]; then
        gcc_version=$(dpkg -l|grep gcc|head -n 1|awk '{print $3}')
    fi
    if echo ${gcc_version}|grep -Eqi '^4.[456789].' ; then
        echo "gcc is 4.4+ Ok"
     elif echo ${gcc_version}|grep -Eqi '^5.' ; then
        echo "gcc is 5.x Ok"
     else
        echo "your gcc lowwer"
        echo "Please make ${Gcc_Ver}"
        cd ${cur_dir}/src
        if [ "$PM" = "yum" ]; then
            yum -y install zip
         elif [ "$PM" = "apt" ]; then
            apt-get -y install zip
        fi
        cd ${cur_dir}/src
        Download_Files ${Download_Mirror}/gcc/${Gcc_Ver}.tar.bz2 ${Gcc_Ver}.tar.bz2
        Download_Files ${Download_Mirror}/gcc/${Gmp_Ver}.tar.bz2 ${Gmp_Ver}.tar.bz2
        Download_Files ${Download_Mirror}/gcc/${Mpc_Ver}.tar.gz ${Mpc_Ver}.tar.gz
        Download_Files ${Download_Mirror}/gcc/${Mpfr_Ver}.tar.bz2 ${Mpfr_Ver}.tar.bz2
        Download_Files ${Download_Mirror}/gcc/${Isl_Ver}.tar.bz2 ${Isl_Ver}.tar.bz2
        tar jxf ${Gcc_Ver}.tar.bz2
        tar jxf ${Gmp_Ver}.tar.bz2
        tar zxf ${Mpc_Ver}.tar.gz
        tar jxf ${Mpfr_Ver}.tar.bz2
        tar jxf ${Isl_Ver}.tar.bz2
        mv ${Gmp_Ver} ${Gcc_Ver}/gmp
        mv ${Mpc_Ver} ${Gcc_Ver}/mpc
        mv ${Mpfr_Ver} ${Gcc_Ver}/mpfr
        mv ${Isl_Ver} ${Gcc_Ver}/isl
        cd ${Gcc_Ver}
        if [ "${Is_64bit}" = "y" ] ; then
            ./configure --prefix=/usr --disable-multilib
         else
            ./configure --prefix=/usr
        fi
        make && make install
        ldconfig
        source /etc/profile
        cd ${cur_dir}/src/
        rm -rf ${Gcc_Ver}
    fi
}

Install_cmake()
{
    if [ "$PM" = "yum" ]; then
        cmake_version=$(rpm -qa|grep cmake|head -n 1|awk -F "-" '{print $2}')
     elif [ "$PM" = "apt" ]; then
        cmake_version=$(dpkg -l|grep cmake|head -n 1|awk '{print $3}')
    fi
    if echo ${cmake_version}|grep -Eqi '^2.[123]' ; then
        cd ${cur_dir}/src
        Download_Files ${Download_Mirror}/cmake/${cmake_Ver}.tar.gz ${cmake_Ver}.tar.gz
        Tar_Cd ${cmake_Ver}.tar.gz ${cmake_Ver}
        ./configure
        make && make install
        ldconfig
        cd ${cur_dir}/src/
        rm -rf ${cmake_Ver}
    fi
}

CentOS_Lib_Opt()
{
    if [ "${Is_64bit}" = "y" ] ; then
        ln -s /usr/lib64/libpng.* /usr/lib/
        ln -s /usr/lib64/libjpeg.* /usr/lib/
    fi
    ulimit -v unlimited
    if [ `grep -L "/lib"    '/etc/ld.so.conf'` ]; then
        echo "/lib" >> /etc/ld.so.conf
    fi
    if [ `grep -L '/usr/lib'    '/etc/ld.so.conf'` ]; then
        echo "/usr/lib" >> /etc/ld.so.conf
        #echo "/usr/lib/openssl/engines" >> /etc/ld.so.conf
    fi
    if [ -d "/usr/lib64" ] && [ `grep -L '/usr/lib64'    '/etc/ld.so.conf'` ]; then
        echo "/usr/lib64" >> /etc/ld.so.conf
        #echo "/usr/lib64/openssl/engines" >> /etc/ld.so.conf
    fi
    if [ `grep -L '/usr/local/lib'    '/etc/ld.so.conf'` ]; then
        echo "/usr/local/lib" >> /etc/ld.so.conf
    fi
    ldconfig
    if ! cat /etc/security/limits.conf | grep -Eqi "soft nproc 65535" ; then
        cat >>/etc/security/limits.conf<<eof
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
eof
    fi
    if ! cat /etc/sysctl.conf | grep -Eqi fs.file-max=65535 ; then
        echo "fs.file-max=65535" >> /etc/sysctl.conf
    fi
    if ! cat /etc/sysctl.conf | grep vm.nr_hugepages ; then
        echo "vm.nr_hugepages=64" >> /etc/sysctl.conf
    fi
    sysctl vm.nr_hugepages=64
}

Deb_Lib_Opt()
{
    if [ "${Is_64bit}" = "y" ] ; then
        ln -s /usr/lib/x86_64-linux-gnu/libpng* /usr/lib/
        ln -s /usr/lib/x86_64-linux-gnu/libjpeg* /usr/lib/
      else
        ln -s /usr/lib/i386-linux-gnu/libpng* /usr/lib/
        ln -s /usr/lib/i386-linux-gnu/libjpeg* /usr/lib/
        ln -s /usr/include/i386-linux-gnu/asm /usr/include/asm
    fi
    ulimit -v unlimited
    if [ `grep -L "/lib"    '/etc/ld.so.conf'` ]; then
        echo "/lib" >> /etc/ld.so.conf
    fi
    if [ `grep -L '/usr/lib'    '/etc/ld.so.conf'` ]; then
        echo "/usr/lib" >> /etc/ld.so.conf
    fi
    if [ -d "/usr/lib64" ] && [ `grep -L '/usr/lib64'    '/etc/ld.so.conf'` ]; then
        echo "/usr/lib64" >> /etc/ld.so.conf
    fi
    if [ `grep -L '/usr/local/lib'    '/etc/ld.so.conf'` ]; then
        echo "/usr/local/lib" >> /etc/ld.so.conf
    fi
    ldconfig
    if ! cat /etc/security/limits.conf | grep -Eqi "soft nproc 65535" ; then
        cat >>/etc/security/limits.conf<<eof
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
eof
    fi
    if ! cat /etc/sysctl.conf | grep -Eqi fs.file-max=65535 ; then
        echo "fs.file-max=65535" >> /etc/sysctl.conf
    fi
    if ! cat /etc/sysctl.conf | grep vm.nr_hugepages ; then
        echo "vm.nr_hugepages=64" >> /etc/sysctl.conf
    fi
    sysctl vm.nr_hugepages=64
}

MySQL_Opt()
{
    if [[ ${MemTotal} -gt 1024 && ${MemTotal} -lt 2048 ]]; then
        sed -i "s#^key_buffer_size.*#key_buffer_size = 32M#" /etc/my.cnf
        sed -i "s#^table_open_cache.*#table_open_cache = 128#" /etc/my.cnf
        sed -i "s#^sort_buffer_size.*#sort_buffer_size = 768K#" /etc/my.cnf
        sed -i "s#^read_buffer_size.*#read_buffer_size = 768K#" /etc/my.cnf
        sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 8M#" /etc/my.cnf
        sed -i "s#^thread_cache_size.*#thread_cache_size = 16#" /etc/my.cnf
        sed -i "s#^query_cache_size.*#query_cache_size = 16M#" /etc/my.cnf
        sed -i "s#^tmp_table_size.*#tmp_table_size = 32M#" /etc/my.cnf
        sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 128M#" /etc/my.cnf
        sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 32M#" /etc/my.cnf
     elif [[ ${MemTotal} -ge 2048 && ${MemTotal} -lt 4096 ]]; then
        sed -i "s#^key_buffer_size.*#key_buffer_size = 64M#" /etc/my.cnf
        sed -i "s#^table_open_cache.*#table_open_cache = 256#" /etc/my.cnf
        sed -i "s#^sort_buffer_size.*#sort_buffer_size = 1M#" /etc/my.cnf
        sed -i "s#^read_buffer_size.*#read_buffer_size = 1M#" /etc/my.cnf
        sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 16M#" /etc/my.cnf
        sed -i "s#^thread_cache_size.*#thread_cache_size = 32#" /etc/my.cnf
        sed -i "s#^query_cache_size.*#query_cache_size = 32M#" /etc/my.cnf
        sed -i "s#^tmp_table_size.*#tmp_table_size = 64M#" /etc/my.cnf
        sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 256M#" /etc/my.cnf
        sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 64M#" /etc/my.cnf
     elif [[ ${MemTotal} -ge 4096 && ${MemTotal} -lt 8192 ]]; then
        sed -i "s#^key_buffer_size.*#key_buffer_size = 128M#" /etc/my.cnf
        sed -i "s#^table_open_cache.*#table_open_cache = 512#" /etc/my.cnf
        sed -i "s#^sort_buffer_size.*#sort_buffer_size = 2M#" /etc/my.cnf
        sed -i "s#^read_buffer_size.*#read_buffer_size = 2M#" /etc/my.cnf
        sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 32M#" /etc/my.cnf
        sed -i "s#^thread_cache_size.*#thread_cache_size = 64#" /etc/my.cnf
        sed -i "s#^query_cache_size.*#query_cache_size = 64M#" /etc/my.cnf
        sed -i "s#^tmp_table_size.*#tmp_table_size = 64M#" /etc/my.cnf
        sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 512M#" /etc/my.cnf
        sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 128M#" /etc/my.cnf
     elif [[ ${MemTotal} -ge 8192 ]]; then
        sed -i "s#^key_buffer_size.*#key_buffer_size = 256M#" /etc/my.cnf
        sed -i "s#^table_open_cache.*#table_open_cache = 1024#" /etc/my.cnf
        sed -i "s#^sort_buffer_size.*#sort_buffer_size = 4M#" /etc/my.cnf
        sed -i "s#^read_buffer_size.*#read_buffer_size = 4M#" /etc/my.cnf
        sed -i "s#^myisam_sort_buffer_size.*#myisam_sort_buffer_size = 64M#" /etc/my.cnf
        sed -i "s#^thread_cache_size.*#thread_cache_size = 128#" /etc/my.cnf
        sed -i "s#^query_cache_size.*#query_cache_size = 128M#" /etc/my.cnf
        sed -i "s#^tmp_table_size.*#tmp_table_size = 128M#" /etc/my.cnf
        sed -i "s#^innodb_buffer_pool_size.*#innodb_buffer_pool_size = 1024M#" /etc/my.cnf
        sed -i "s#^innodb_log_file_size.*#innodb_log_file_size = 256M#" /etc/my.cnf
    fi
}

LNMP_PHP_Opt()
{
    Get_PHP_Ext_Dir
    if [[ ${MemTotal} -le 1024 ]]; then
        PHPFPM_Max=10
        PHPFPM_Min=4
        PHPFPM_Start=$(($PHPFPM_Min+($PHPFPM_Max-$PHPFPM_Min)/2))
     elif [[ ${MemTotal} -gt 1024 && ${MemTotal} -le 2048 ]]; then
        PHPFPM_Max=20
        PHPFPM_Min=10
        PHPFPM_Start=$(($PHPFPM_Min+($PHPFPM_Max-$PHPFPM_Min)/2))
     elif [[ ${MemTotal} -gt 2048 && ${MemTotal} -le 4096 ]]; then
        PHPFPM_Max=40
        PHPFPM_Min=20
        PHPFPM_Start=$(($PHPFPM_Min+($PHPFPM_Max-$PHPFPM_Min)/2))
     elif [[ ${MemTotal} -gt 4096 && ${MemTotal} -le 8192 ]]; then
        PHPFPM_Max=60
        PHPFPM_Min=30
        PHPFPM_Start=$(($PHPFPM_Min+($PHPFPM_Max-$PHPFPM_Min)/2))
     elif [[ ${MemTotal} -gt 8192 ]]; then
        PHPFPM_Max=80
        PHPFPM_Min=40
        PHPFPM_Start=$(($PHPFPM_Min+($PHPFPM_Max-$PHPFPM_Min)/2))
    fi
    if echo "${Cur_PHP_Version}" | grep -Eqi '^5.2.';then
        sed -i "s#<value name=\"max_children\">.*#<value name=\"max_children\">${PHPFPM_Max}</value>#" /usr/local/php/etc/php-fpm.conf
        sed -i "s#<value name=\"StartServers\">.*#<value name=\"StartServers\">${PHPFPM_Start}</value>#" /usr/local/php/etc/php-fpm.conf
        sed -i "s#<value name=\"MinSpareServers\">.*#<value name=\"MinSpareServers\">${PHPFPM_Min}</value>#" /usr/local/php/etc/php-fpm.conf
        sed -i "s#<value name=\"MaxSpareServers\">.*#<value name=\"MaxSpareServers\">${PHPFPM_Max}</value>#" /usr/local/php/etc/php-fpm.conf
     else
        sed -i "s#pm.max_children.*#pm.max_children = ${PHPFPM_Max}#" /usr/local/php/etc/php-fpm.conf
        sed -i "s#pm.start_servers.*#pm.start_servers = ${PHPFPM_Start}#" /usr/local/php/etc/php-fpm.conf
        sed -i "s#pm.min_spare_servers.*#pm.min_spare_servers = ${PHPFPM_Min}#" /usr/local/php/etc/php-fpm.conf
        sed -i "s#pm.max_spare_servers.*#pm.max_spare_servers = ${PHPFPM_Max}#" /usr/local/php/etc/php-fpm.conf
    fi
}

Add_LNMP_Startup()
{
    echo "Add Startup and Starting LNMP..."
    \cp ${cur_dir}/conf/lnmp /bin/lnmp
    chmod +x /bin/lnmp
    StartUp nginx
    /etc/init.d/nginx start
    if [ ${DBSelect} -ge 6 ]; then
        StartUp mariadb
        /etc/init.d/mariadb start
     else
        StartUp mysql
        /etc/init.d/mysql start
        sed -i 's#/etc/init.d/mariadb#/etc/init.d/mysql#' /bin/lnmp
    fi
    StartUp php-fpm
    /etc/init.d/php-fpm start
    if [ "${PHPSelect}" = "1" ]; then
        sed -i 's#/usr/local/php/var/run/php-fpm.pid#/usr/local/php/logs/php-fpm.pid#' /bin/lnmp
    fi
}

Add_LNMPA_Startup()
{
    echo "Add Startup and Starting LNMPA..."
    \cp ${cur_dir}/conf/lnmpa /bin/lnmp
    chmod +x /bin/lnmp
    StartUp nginx
    /etc/init.d/nginx start
    if [ ${DBSelect} -ge 6 ]; then
        StartUp mariadb
        /etc/init.d/mariadb start
     else
        StartUp mysql
        /etc/init.d/mysql start
        sed -i 's#/etc/init.d/mariadb#/etc/init.d/mysql#' /bin/lnmp
    fi
    StartUp httpd
    /etc/init.d/httpd start
}

Add_LAMP_Startup()
{
    echo "Add Startup and Starting LAMP..."
    \cp ${cur_dir}/conf/lamp /bin/lnmp
    chmod +x /bin/lnmp
    StartUp httpd
    /etc/init.d/httpd start
    if [ ${DBSelect} -ge 6 ]; then
        StartUp mariadb
        /etc/init.d/mariadb start
     else
        StartUp mysql
        /etc/init.d/mysql start
        sed -i 's#/etc/init.d/mariadb#/etc/init.d/mysql#' /bin/lnmp
    fi
}

Check_Nginx_Files()
{
    echo "Checking ..."
    isNginx=""
    if [[ -s /usr/local/nginx/conf/nginx.conf && -s /usr/local/nginx/sbin/nginx ]]; then
        Echo_Green "Nginx: OK"
        isNginx="ok"
     else
        Echo_Red "Error: Nginx install failed."
    fi
}

Check_DB_Files()
{
    isDB=""
    if [[ -s /usr/local/mysql/bin/mysql && -s /usr/local/mysql/bin/mysqld_safe && -s /etc/my.cnf ]]; then
        Echo_Green "Mysql: OK"
        isDB="ok"
     elif [[ -s /usr/local/mariadb/bin/mysql && -s /usr/local/mariadb/bin/mysqld_safe && -s /etc/my.cnf ]]; then
        Echo_Green "MariaDB: OK"
        isDB="ok"
     else
        Echo_Red "Error: MariaDB/Mysql isn't installed."
    fi
}

Check_PHP_Files()
{
    isPHP=""
    if [ "${Stack}" = "lnmp" ]; then
        if [[ -s /usr/local/php/sbin/php-fpm && -s /usr/local/php/etc/php.ini && -s /usr/local/php/bin/php ]]; then
            Echo_Green "PHP: OK"
            Echo_Green "PHP-FPM: OK"
            isPHP="ok"
         else
            Echo_Red "Error: PHP install failed."
        fi
     else
        if [[ -s /usr/local/php/bin/php && -s /usr/local/php/etc/php.ini ]]; then
            Echo_Green "PHP: OK"
            isPHP="ok"
         else
            Echo_Red "Error: PHP install failed."
        fi
    fi
}

Check_Apache_Files()
{
    isApache=""
    if [[ -s /usr/local/apache/bin/httpd && ( -s /usr/local/apache/modules/libphp5.so || -s /usr/local/apache/modules/libphp7.so ) && -s /usr/local/apache/conf/httpd.conf ]]; then
        Echo_Green "Apache: OK"
        isApache="ok"
     else
        Echo_Red "Error: Apache install failed."
    fi
}

Print_Sucess_Info()
{
    echo "+------------------------------------------------------------------------"
    echo "|          LNMP V${LNMP_Ver} for ${DISTRO} Linux Server, Written by Hongm"
    echo "+------------------------------------------------------------------------"
    echo "|         For more information please visit http://hongm.emoodle.org"
    echo "+------------------------------------------------------------------------"
    echo "|    lnmp status manage: lnmp {start|stop|reload|restart|kill|status}"
    echo "+------------------------------------------------------------------------"
    echo "|  phpMyAdmin: http://IP/phpmyadmin/"
    echo "|  phpinfo: http://IP/phpinfo.php"
    echo "|  Prober:  http://IP/p.php"
    echo "+------------------------------------------------------------------------"
    echo "|  Add VirtualHost: lnmp vhost add"
    echo "+------------------------------------------------------------------------"
    echo "|  Default directory: /home/wwwroot/default"
    echo "+------------------------------------------------------------------------"
    echo "|  MySQL/MariaDB root password: ${DB_Root_Password}"
    echo "+------------------------------------------------------------------------"
    lnmp status
    netstat -ntl  
    Echo_Green "Install lnmp V${LNMP_Ver} completed! enjoy it."
}

Print_Failed_Info()
{
    Echo_Red "Sorry,Failed to install LNMP!"
    Echo_Red "You can download /root/lnmp-install.log from your server,and mail lnmp-install.log to hongm@sina.com for help."
}

Check_LNMP_Install()
{
    Check_Nginx_Files
    Check_DB_Files
    Check_PHP_Files
    if [[ "$isNginx" = "ok" && "$isDB" = "ok" && "$isPHP" = "ok" ]]; then
        Print_Sucess_Info
     else
        Print_Failed_Info
    fi
}

Check_LNMPA_Install()
{
    Check_Nginx_Files
    Check_DB_Files
    Check_PHP_Files
    Check_Apache_Files
    if [[ "$isNginx" = "ok" && "$isDB" = "ok" && "$isPHP" = "ok"  &&"$isApache" = "ok" ]]; then
        Print_Sucess_Info
     else
        Print_Failed_Info
    fi
}

Check_LAMP_Install()
{
    Check_Apache_Files
    Check_DB_Files
    Check_PHP_Files
    if [[ "$isApache" = "ok" && "$isDB" = "ok" && "$isPHP" = "ok" ]]; then
        Print_Sucess_Info
     else
        Print_Failed_Info
    fi
}

Check_Libtidy()
{
    if [[ -s /usr/lib/libtidys.so && -s /usr/include/tidy/buffio.h ]]; then
        Echo_Green "Libtidy ...ok"
     else
        Install_Libtidy
    fi
}

Check_Autoconf()
{
    if [[ -s /usr/local/autoconf/bin/autoconf && -s /usr/local/autoconf/bin/autoheader ]]; then
        Echo_Green "Autconf ...ok"
        export PHP_AUTOCONF=/usr/local/autoconf/bin/autoconf
        export PHP_AUTOHEADER=/usr/local/autoconf/bin/autoheader
     else
        Install_Autoconf
    fi
}

Check_Curl()
{
    if [ -s /usr/local/curl/bin/curl ]; then
        Echo_Green "Curl ...ok"
     else
        Install_Curl
    fi
}